from django.db import models
from django.contrib.auth.models import AbstractUser


class SysRole(models.Model):
    """
    系统角色模型类，用于管理角色信息。
    
    属性:
    - id: 自动增长的主键
    - name: 角色名称，唯一标识
    - description: 角色描述
    - sort: 显示顺序
    - status: 角色状态，0正常 1停用
    - create_time: 创建时间
    - update_time: 更新时间
    - remark: 备注
    """
    id = models.AutoField(primary_key=True)
    name = models.CharField(max_length=50, unique=True, verbose_name="角色名称")
    description = models.CharField(max_length=200, null=True, blank=True, verbose_name="角色描述")
    sort = models.IntegerField(default=0, verbose_name="显示顺序")
    status = models.IntegerField(default=0, verbose_name="角色状态（0正常 1停用）")
    create_time = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")
    update_time = models.DateTimeField(auto_now=True, verbose_name="更新时间")
    remark = models.CharField(max_length=500, null=True, blank=True, verbose_name="备注")
    
    class Meta:
        db_table = "sys_role"
        verbose_name = "角色管理"
        ordering = ['sort', 'id']


class SysPermission(models.Model):
    """
    系统权限模型类，用于管理权限信息。
    
    属性:
    - id: 自动增长的主键
    - name: 权限名称
    - code: 权限编码，唯一标识
    - parent_id: 父级权限ID
    - type: 权限类型，M目录 C菜单 F按钮
    - path: 路由地址
    - icon: 菜单图标
    - component: 组件路径
    - sort: 显示顺序
    - status: 权限状态，0正常 1停用
    - create_time: 创建时间
    - update_time: 更新时间
    """
    id = models.AutoField(primary_key=True)
    name = models.CharField(max_length=50, verbose_name="权限名称")
    code = models.CharField(max_length=100, unique=True, verbose_name="权限编码")
    parent_id = models.IntegerField(default=0, verbose_name="父级权限ID")
    type = models.CharField(max_length=1, choices=[('M', '目录'), ('C', '菜单'), ('F', '按钮')], default='C', verbose_name="权限类型")
    path = models.CharField(max_length=200, null=True, blank=True, verbose_name="路由地址")
    icon = models.CharField(max_length=100, null=True, blank=True, verbose_name="菜单图标")
    component = models.CharField(max_length=200, null=True, blank=True, verbose_name="组件路径")
    sort = models.IntegerField(default=0, verbose_name="显示顺序")
    status = models.IntegerField(default=0, verbose_name="权限状态（0正常 1停用）")
    create_time = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")
    update_time = models.DateTimeField(auto_now=True, verbose_name="更新时间")
    
    class Meta:
        db_table = "sys_permission"
        verbose_name = "权限管理"
        ordering = ['sort', 'id']


class SysRolePermission(models.Model):
    """
    角色权限关联模型类
    """
    id = models.AutoField(primary_key=True)
    role = models.ForeignKey(SysRole, on_delete=models.CASCADE, verbose_name="角色")
    permission = models.ForeignKey(SysPermission, on_delete=models.CASCADE, verbose_name="权限")
    create_time = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")
    
    class Meta:
        db_table = "sys_role_permission"
        verbose_name = "角色权限关联"
        unique_together = ('role', 'permission')


class SysUserRole(models.Model):
    """
    用户角色关联模型类
    """
    id = models.AutoField(primary_key=True)
    user_id = models.IntegerField(verbose_name="用户ID")
    role = models.ForeignKey(SysRole, on_delete=models.CASCADE, verbose_name="角色")
    create_time = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")
    
    class Meta:
        db_table = "sys_user_role"
        verbose_name = "用户角色关联"
        unique_together = ('user_id', 'role')
